<?php
require 'Admin/Model/Model.php';
class studentModel extends Model{
	function __Construct(){
		parent::__Construct();
		
	}
	function getList(){
			/*$data = array(
			array('uname'=>'jason','gender'=>'男'),
			array('uname'=>'jack','gender'=>'男')
			);*/
			$sql = "select * from {$this->mod}";
			$data = $this->conn->query($sql);
			return $data;
		}
		function add($data)
		{
			if(!empty($_FILES["pic"]["name"])){
				$pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
			}
			else{
				$pic='';
			}
			$student = array(
				'sname' => trim($data['sname']),  
				'password' => $data['password'],
				'gender' => $data['gender'],
				'birthdate' => $data['birthdate'],
				'hobby' => implode($data['hobby'], ','),
				'degree' => $data['degree'],
				'intro' => $data['intro'],
				'pic'=>$pic
				);
			$keys = implode(',',array_keys($student));
			$values = implode("','", array_values($student));
			$sql="insert into student ({$keys}) VALUES ('{$values}')";
			  //执行SQL语句
			$result = $this->conn->query($sql);
			return $result;
		}
		function getStudentByID($id){
			$sql = "select * from student where sid=".$id;
			$result = $this->conn->query($sql);
			return $result;
		}
		function update($data)
		{
			$student = array(
				'sname' => trim($data['sname']),  
					
				'gender' => $data['gender'],
				'birthdate' => $data['birthdate'],
				'hobby' => implode($data['hobby'], ','),
				'degree' => $data['degree'],
				'intro' => $data['intro']
				);
			if(!empty($_FILES["pic"]["name"])){
				$pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
				$student['pic'] = $pic;
			}
			
			
			$sets = '';
			foreach ($student as $key=>$val){
				$sets.=$key."='".$val."',";
			}
			    $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号
			    
			    $sql="UPDATE student SET {$sets} where sid=".$data['id'];
			  //执行SQL语句
			    $result = $this->conn->query($sql);
			    return $result;		    
			}
			function delete($id)
			{
				$sql = "delete from student where sid=".$id;
				$result = $this->conn->query($sql);
				return $result;
			}




		}
		?>